package com.ssm.controller;

import com.ssm.entity.PageBean;
import com.ssm.entity.Picture;
import com.ssm.service.PictureService;
import com.ssm.utils.ResponseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by LinYaTing on 2018/12/9 13:18
 */
@Controller
@RequestMapping(value = "/pictures")
public class PictureController {

    @Autowired
    private PictureService pictureService;

    private static final long serialVersionUID = 1L;

    private static final Logger log = Logger.getLogger(PictureController.class);// 日志文件

    @RequestMapping(value = "/datagrid")
    public String list(@RequestParam(value = "page", required = false) String page,
                       @RequestParam(value = "rows",required = false) String rows, Picture picture, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<>();
        if (page !=null && rows !=null) {
            PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
            map.put("start",pageBean.getStart());
            map.put("size", pageBean.getPageSize());
        }
        if (picture !=null) {
            map.put("id",picture.getId() + "");
            map.put("type",picture.getType() + "");
            map.put("grade", picture.getGrade() + "");
        }
        List<Picture> pictureList = pictureService.findPicture(map);
        Long total = pictureService.getTotalPicture(map);
        JSONObject result = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(pictureList);
        result.put("rows", jsonArray);
        result.put("total", total);
        log.info("request: pictures/datagrid , map: " + map.toString());
        ResponseUtil.write(response, result);
        return null;
    }
}
